package com.good.child.ui.adapter.ai;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.good.child.R;
import com.good.child.bean.ai.AIHelperHistoryBean;
import com.good.child.widgets.DotsProgress;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AiGeneratePictureAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<AIHelperHistoryBean> dataList;
    private onItemClick onItemClick;
    public static final int ITEM_TYPE_SEND = 1;
    public static final int ITEM_TYPE_RECEIVE = 2;
    public static final int ITEM_TYPE_LOADING = 3;

    public AiGeneratePictureAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<AIHelperHistoryBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setOnItemClick(onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_SEND) {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_generate_picture_send, parent, false);
            AiGeneratePictureAdapter.SendViewHolder myViewHolder = new AiGeneratePictureAdapter.SendViewHolder(inflater);
            return myViewHolder;
        } else if (viewType == ITEM_TYPE_RECEIVE) {
           /* View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_generate_picture_receive, parent, false);
            AiGeneratePictureAdapter.ReceiveViewHolder myViewHolder = new AiGeneratePictureAdapter.ReceiveViewHolder(inflater);
  */
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_generate_picture_receive_new, parent, false);
            AiGeneratePictureAdapter.ReceiveViewHolderNew myViewHolder = new AiGeneratePictureAdapter.ReceiveViewHolderNew(inflater);
            return myViewHolder;
        } else {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_message_loading, parent, false);
            AiGeneratePictureAdapter.LoadingViewHolder myViewHolder = new AiGeneratePictureAdapter.LoadingViewHolder(inflater);
            return myViewHolder;
        }
    }

    @Override
    public int getItemViewType(int position) {
        AIHelperHistoryBean bean = dataList.get(position);
        return bean.isLoading() ? ITEM_TYPE_LOADING : (bean.getModeType() == 2 ? ITEM_TYPE_RECEIVE : ITEM_TYPE_SEND);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (dataList == null || dataList.isEmpty()) {
            return;
        }
        AIHelperHistoryBean bean = dataList.get(position);
        if (holder instanceof SendViewHolder) {
            SendViewHolder viewHolder = (SendViewHolder) holder;
            viewHolder.tvContent.setText(bean.getContent());
        } else if (holder instanceof ReceiveViewHolderNew) {
            if (TextUtils.isEmpty(bean.getImageUrl())) {
                return;
            }
            Uri uri = Uri.parse(bean.getImageUrl());
            // Uri uri = Uri.parse("http://tongban-ai.oss-cn-shanghai.aliyuncs.com/877102528423137280.jpeg?Expires=1723886416&OSSAccessKeyId=LTAI5tFhxacsvPaFy9EoW6MF&Signature=xQWmOenV9z5zgtPsd62qVVe4UTQ%3D;http://tongban-ai.oss-cn-shanghai.aliyuncs.com/877102537537359872.jpeg?Expires=1723886416&OSSAccessKeyId=LTAI5tFhxacsvPaFy9EoW6MF&Signature=0FMjn1M9w1sx59DxoyFh2aE8AJA%3D;http://tongban-ai.oss-cn-shanghai.aliyuncs.com/877102538103590912.jpeg?Expires=1723886416&OSSAccessKeyId=LTAI5tFhxacsvPaFy9EoW6MF&Signature=zcq9GRErH8avNDejjIUgY%2FV33Ds%3D;http://tongban-ai.oss-cn-shanghai.aliyuncs.com/877102538502049792.jpeg?Expires=1723886416&OSSAccessKeyId=LTAI5tFhxacsvPaFy9EoW6MF&Signature=j6M4geMq7rf2Di%2FoUz3VddhAcT8%3D;http://tongban-ai.oss-cn-shanghai.aliyuncs.com/877102538837594112.jpeg?Expires=1723886416&OSSAccessKeyId=LTAI5tFhxacsvPaFy9EoW6MF&Signature=iG25uv9UyuFFaB7aYzwF7kMm9D8%3D");
            ReceiveViewHolderNew viewHolderNew = (ReceiveViewHolderNew) holder;
            Glide.with(context).load(uri).into(viewHolderNew.im);
            viewHolderNew.im.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClick != null) {
                        onItemClick.onClickView(bean, viewHolderNew.im);
                    }
                }
            });
           /* List<String>list=new ArrayList<>();
            list.add(bean.getImageUrl());
            ReceiveViewHolder viewHolder= (ReceiveViewHolder) holder;
            AiPictureAdapter aiPictureAdapter=new AiPictureAdapter(context);
            StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            viewHolder.recyclerview.setLayoutManager(staggeredGridLayoutManager);
            viewHolder.recyclerview.setAdapter(aiPictureAdapter);
           aiPictureAdapter.setData(list);*/
        } else {
            LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
            loadingViewHolder.dotsProgress.startProgress();
        }
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 1 : dataList.size();
    }

    class SendViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_content)
        TextView tvContent;

        public SendViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ReceiveViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.recyclerview)
        RecyclerView recyclerview;

        public ReceiveViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ReceiveViewHolderNew extends RecyclerView.ViewHolder {
        @BindView(R.id.im)
        ImageView im;

        public ReceiveViewHolderNew(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class LoadingViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.dot)
        DotsProgress dotsProgress;

        public LoadingViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface onItemClick {
        void onClickView(AIHelperHistoryBean bean, View view);
    }
}
